package oms.controllers;

import java.util.Map;
import oms.entities.User;
import oms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value = "/service")
public class AdminController {
	
		//@Autowired
		private UserService userService;
	
		
		// Create User
		@RequestMapping(value = "/users/add", method = RequestMethod.POST)
		@ResponseBody
		public  String addUser(@ModelAttribute("user") User user) {
		
				userService.createUser(user);
				return "redirect:/"; 
		}
	
		
		// Delete User
		@RequestMapping("/users/delete/{userId}")
		public String deleteUser(@PathVariable("usertId") Long userId) {

				userService.deleteUser(userId);
				return "redirect:/"; 
		}
	
		// Update User
		@RequestMapping("/users/update/{userId}")
		public String updateUser(@PathVariable("usertId") Long userId) {
				
			userService.updateUser(userId);
			return "redirect:/"; 
		}
	
		// Users List
		@RequestMapping("/service")
		public String listUsers(Map<String, Object> map) {

				map.put("user", new User());
				map.put("usersList", userService.usersList());

				return "user";
		}
		
		
		
}
